coreExam.editPage = {

    lockExam: function () {
        var selectedRows = Gikam.getComp('core-exam-enable-list-grid').getSelections();
        if (Gikam.isEmpty(selectedRows)) {
            Gikam.alert("GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM");
            return;
        }

        var url = coreExam.baseUrl + '/action/activate';

        workspace.window.showMask(true);
        Gikam.put(url, Gikam.getJsonWrapper(null, ['', selectedRows])).done(function (r) {
            Gikam.getComp('core-exam-enable-list-grid').refresh();
            Gikam.getComp('core-exam-disable-list-grid').refresh();
        }).always(function () {
            workspace.window.closeMask();
        });
    },

    getEnableGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'core-exam-enable-list-grid',
            url: coreExam.baseUrl + '/queries/raw',
            requestData: {
                activatedFlag_EQ: '0'
            },
            deleteFormatter: function (row) {
                return row.examName || row.id;
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'CORE.MODULE.TRAIN.TRAIN.BUTTON.ADD',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'CORE.MODULE.TRAIN.EXAM.MODAL.TITLE.ADD_EXAM',
                        fields: coreExam.getBaseInfoFormFields(),
                        url: coreExam.baseUrl,
                        titleWidth: 100,
                        param: {
                            questionMethod: 'manual'
                        }
                    }).done(function (id) {
                        _this.loadDetailPage(id);
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                onClick: function () {
                    Gikam.getComp('core-exam-enable-list-grid').deleteRows(coreExam.baseUrl);
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.ACTIVE',
                icon: 'enable',
                onClick: function () {
                    _this.lockExam();
                }
            }],
            toolbarAlign: 'left',
            columns: coreExam.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'examName' && _this.loadDetailPage(row.id);
            }
        }
    },

    getDisableGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'core-exam-disable-list-grid',
            url: coreExam.baseUrl + '/queries/raw',
            requestData: {
                activatedFlag_EQ: '1'
            },
            page: true,
            deleteFormatter: function (row) {
                return row.examName || row.id;
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.DEACTIVE',
                icon: 'disable',
                onClick: function () {
                    var selectedRows = Gikam.getComp('core-exam-disable-list-grid').getSelections();
                    if (Gikam.isEmpty(selectedRows)) {
                        Gikam.alert("GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM");
                        return;
                    }

                    let url = coreExam.baseUrl + '/action/deactivate';
                    workspace.window.showMask(true);
                    Gikam.put(url, Gikam.getJsonWrapper(null, ['', selectedRows])).done(function () {
                        Gikam.getComp('core-exam-enable-list-grid').refresh();
                        Gikam.getComp('core-exam-disable-list-grid').refresh();
                    }).always(function () {
                        workspace.window.closeMask();
                    });

                }
            }],
            toolbarAlign: 'left',
            columns: coreExam.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'examName' && _this.loadDetailPage(row.id);
            }
        }
    },

    getGridTabParam: function () {
        let _this = this;
        return {
            type: 'tab',
            id: 'core-exam-tab',
            panels: [{
                title: 'GIKAM.TAB.TITLE.NO.ENABLE',
                items: [_this.getEnableGridParam()]
            }, {
                title: 'GIKAM.TAB.TITLE.ENABLE',
                items: [_this.getDisableGridParam()]
            }]
        }
    },

    create: function () {
        let _this = this;
        Gikam.create('layout', {
            id: 'core-exam-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [_this.getGridTabParam()]
            }
        });
    },

    loadDetailPage: function (id) {
        workspace.window.load(coreExam.baseUrl + '/page/detail' + Gikam.param({
            id: id
        }));
    },

    init: function (param) {
        var _this = this;
        _this.param = param;
        _this.create();
    }
};